// pages/news/detail.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');
var wxParse = require('../../wxParse/wxParse.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    fontSize: 16,
    id: 0,
    detail: {},
    user: {},
    article_content: {},
    loaddinghidden: true,
    showLoading: false,
    isEditShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 页面初始化 options 为页面跳转所带来的参数
    var that = this;
    var id = options.id;
    if (!id) {
      wx.showModal({
        title: '加载失败！',
        success: function (res) {
          if (res.confirm) {
            wx.navigateBack();
          }
        }
      })
      return;
    }

    /**
     * 获取系统信息
     */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    // 显示 loading
    that.setData({
      id: id,
      loaddinghidden: false
    });

    that.loadData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var fontSize = wx.getStorageSync("fontSize");

    if (fontSize) {
      that.setData({
        fontSize: fontSize,
      });
    }
  },
  loadData: function () {
    var that = this;
    var parameter = {};
    parameter['id'] = that.data.id;
    //获取数据
    http.ApiGet(apiUrl.UrlNewsDetail, parameter, function (res) {
      wx.stopPullDownRefresh() //停止下拉刷新
      if (res.result.errorCode == 1) {

        var content = res.data.content;
        if (res.data.content) {


          var article_content = content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;padding-top:2px;" ')
            .replace(/<section/g, '<div')
            .replace(/\/section>/g, '\div>');

          var body = content.replace(/&quot;/g, '"').replace(/&amp;/g, '&')
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>')
            .replace(/<</g, '《')
            .replace(/>>/g, '》')
            .replace(/&nbsp;/g, ' ')
            .replace(/&rdquo;/g, '”')
            .replace(/&ldquo;/g, '“')
            .replace(/&mdash;/g, '—')
            .replace(/&hellip;/g, '…')
            .replace(/&uarr;/g, '↑')
            .replace(/&darr;/g, '↓')
            .replace(/<!---->/g, '')
            .replace(/<\/source>/g, '')
            .replace(/\r\n/g, '')
            .replace(/<br [\s\S]*?\/>/g, '')
            // .replace(/<p[\s\S]*?>(.*?)<\/p>/gi, '<p>$1</p>')
            .replace(/<img[\s\S]*?src=\"([^\"]*?)\"[\s\S]*?\/>/g, '<img src=\"$1\"\/>')
            .replace(/<span[\s\S]*?>(.*?)<\/span>/gi, '$1')
            .replace(/<section[\s\S]*?>(.*?)<\/section>/gi, '$1')
            .replace(/<strong[\s\S]*?>(.*?)<\/strong>/gi, '$1')
            .replace(/<ol[\s\S]*?>(.*?)<\/ol>/gi, '$1')
            .replace(/<script.*?\/script>/g, '')
            .replace(/<iframe.*?\/iframe>/g, '')
            .replace(/<video.*?\/video>/g, '')
            .replace(/<source.*?>/g, '');
        }
        that.setData({
          loaddinghidden: true,
          detail: res.data,
          article_content: article_content,
        });

        var user = wx.getStorageSync("user");
        if (res.data.ymh_id == user.id) {
          that.setData({
            isEditShow: true,
          });
        }
        if (res.data.content) {
          wxParse.wxParse('article', 'html', body, that);
        }
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadData();
  },
  imageTap: function (e) {
    var val = e.currentTarget.dataset.val;
    wx.previewImage({
      current: val,
      urls: this.data.detail.images
    })
  },
  delBtnTap: function () {
    var that = this;
    wx.showModal({
      title: '确定删除？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          http.ApiPost(apiUrl.UrlNewsDel, {
            "id": that.data.id
          }, function (ress) {
            if (ress.result.errorCode == 1) {
              wx.navigateBack();
            } else {
              wx.showModal({
                title: ress.result.errorMessage
              });
            }
          });
        }
      }
    });
  },
  editBtnTap: function () {
    if (this.data.detail.show_type == 1) {
      wx.navigateTo({
        url: '../news/submit?id=' + this.data.id + '&title=修改文章'
      })
    } else if (this.data.detail.show_type == 2) {
      wx.navigateTo({
        url: '../video/submit?id=' + this.data.id + '&title=修改视频'
      })
    } else if (this.data.detail.show_type == 5) {
      wx.navigateTo({
        url: '../dt/submit?id=' + this.data.id + '&title=修改动态'
      })
    }
  }
})